#version 150

in  vec2 texCoord;
out vec4 fColor;

uniform sampler2D texture;

const vec3 LUMCOEFFS = vec3(0.2125, 0.7154, 0.0721);
float T = 0.5;

void main() 
{ 
	ivec2 ires = textureSize(texture,0);
	float ResS = float(ires.s);
	float ResT = float(ires.t);
	vec3 irgb = texture2D(texture, texCoord).rgb;

	vec2 stp0 = vec2(1./ResS, 0.);
	vec2 st0p = vec2(0., 1./ResT);
	vec2 stpp = vec2(1./ResS, 1./ResT);
	vec2 stpm = vec2(1./ResS, -1./ResT);

	float i00 = dot(texture2D(texture, texCoord).rgb, LUMCOEFFS);

	float im1m1 = dot(texture2D(texture, texCoord-stpp).rgb, LUMCOEFFS);
	float ip1p1 = dot(texture2D(texture, texCoord+stpp).rgb, LUMCOEFFS);

	float im1p1 = dot(texture2D(texture, texCoord-stpm).rgb, LUMCOEFFS);
	float ip1m1 = dot(texture2D(texture, texCoord+stpm).rgb, LUMCOEFFS);

	float im10 = dot(texture2D(texture, texCoord-stp0).rgb, LUMCOEFFS);
	float ip10 = dot(texture2D(texture, texCoord+stp0).rgb, LUMCOEFFS);

	float i0m1 = dot(texture2D(texture, texCoord-st0p).rgb, LUMCOEFFS);
	float i0p1 = dot(texture2D(texture, texCoord+st0p).rgb, LUMCOEFFS);

	float h = -1.*im1p1 - 2.*i0p1 - 1.*ip1p1 + 1.*im1m1 + 2.*i0m1 + 1.*ip1m1;
	float v = -1.*im1m1 - 2.*im10 - 1.*im1p1 + 1.*ip1m1 + 2.*ip10 + 1.*ip1p1;

	float mag = sqrt(h*h+v*v);
	vec3 target = vec3(mag, mag, mag);
	fColor = vec4(mix(irgb, target, T), 1.);
}